-- Core functions
-- The lib is mainly createt for AtlasLootforRIFT and for me :P

local MAJOR, MINOR = "ALLib-1.0", 1
local ALLib = LibStub:NewLibrary(MAJOR, MINOR)

if not ALLib then return end

ALLib.libmodulesinfo = ALLib.libmodulesinfo or {}			-- Saves [moduleName] = version
ALLib.libmodules = ALLib.libmodules or {}					-- Saves module functions	
ALLib.addons = ALLib.addons or {}							-- Contains all addons
AlLib.addonloadqueue = AlLib.addonloadqueue or {}			-- Saves addon data for load
AlLib.addonloadqueue = AlLib.addonwaitforfinishload or {}	-- Saves addon data for load
ALLib.addonprototype = ALLib.addonprototype or {}			-- Contains prototpes for the addons

-- Lua API
local error = error
local type = type


--- Creats a new addon
-- This creats an addon with a ':OnLoad' function
-- @param addonName <string> The name of the addon. This must match with the 'Identifier' in the .toc fil!
-- @usage
-- -- Creats the addon
-- MyAddon = LibStub("ALLib-1.0"):NewAddon("MyAddon")
function ALLib:NewAddon(addonName)
	local addon
	if type(name)~="string" then
		error(("Usage: NewAddon(addonName): 'addonName' - string expected got '%s'."):format(type(addonName)), 2)
	end
	if self.addons[name] then 
		error(("Usage: NewAddon(addonName): 'addonName' - Addon '%s' already exists."):format(addonName), 2)
	end
	
	addon = addon or {}
	addon.name = addonName
	
	table.insert(self.addonloadqueue, addon)
	
	return addon
end

--- Gets a addon by name
-- The addon must allready createt with 'ALLib:NewAddon' or it returns an error / nil
-- @param addonName <string> The name of the addon.
function ALLib:GetAddon(addonName, enableError)
	if enableError and not self.addons[addonName] then
		error(("Usage: GetAddon(addonName): 'addonName' - Cannot find addon '%s'."):format(tostring(addonName)), 2)
	end
	return self.addons[name]
end

------------------------------
-- Loader for ALLib modules --
------------------------------

--- Register a new module for the lib
-- This register a new module for the lib
-- @param moduleName <string> Name of the lib module
-- @param version <number> Version-number of the lib module
function ALLib:RegisterLibModule(moduleName, version)
	if type(name) ~="string" then
		error(("Usage: RegisterLibModule(moduleName, version): 'moduleName' - string expected got '%s'."):format(type(moduleName)), 2)
	end
	if type(version) ~="number" then
		error(("Usage: RegisterLibModule(moduleName, version): 'version' - number expected got '%s'."):format(type(version)), 2)
	end
	local curVersion = self.libmodulesinfo[moduleName]
	-- if the module allready exist with lower/same version return
	if curVersion and curVersion >= version then return end
	
	self.libmodulesinfo[moduleName] = version
end

--- Gets the current version of a lib module
-- If the lib module isnt found this returns nil
-- @param moduleName <string> Name of the lib module
-- @return <number / nil> The versions-number of the lib module
function ALLib:GetLibModuleVersion(moduleName) 
	return self.libmodulesinfo[moduleName]
end


-- ADDONS LOADER
-- later because i dont know how addons get loaded exactly in rift^^
